package com.ruoyi.project.module.wtc;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.project.domain.YqgMembershipForm;
import com.ruoyi.project.domain.YqgRechargeConsumption;
import com.ruoyi.project.module.wtc.request.*;
import com.ruoyi.project.service.IYqgMembershipFormService;
import com.ruoyi.project.service.IYqgRechargeConsumptionService;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;

/**
 * 会员Controller
 *
 * @author dr
 * @date 2023-06-01
 */
@RestController
@RequestMapping("/wtc/form")
public class WtcYqgMembershipFormController extends BaseController {
    @Autowired
    private IYqgMembershipFormService yqgMembershipFormService;

    @Autowired
    private IYqgRechargeConsumptionService yqgRechargeConsumptionService;

    /**
     * 查询会员列表
     */
    //@RequiresPermissions("project:form:list")
    @GetMapping("/list")
    public TableDataInfo list(YqgMembershipForm yqgMembershipForm) {
        //startPage();
        List<YqgMembershipForm> list = yqgMembershipFormService.selectYqgMembershipFormListByDesc(yqgMembershipForm);
        return getDataTable(list);
    }

    /**
     * 导出会员列表
     */
    //@RequiresPermissions("project:form:export")
    @Log(title = "会员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqgMembershipForm yqgMembershipForm) {
        List<YqgMembershipForm> list = yqgMembershipFormService.selectYqgMembershipFormList(yqgMembershipForm);
        ExcelUtil<YqgMembershipForm> util = new ExcelUtil<YqgMembershipForm>(YqgMembershipForm.class);
        util.exportExcel(response, list, "会员数据");
    }

    /**
     * 获取会员详细信息
     */
    //@RequiresPermissions("project:form:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        if (yqgMembershipFormService.selectYqgMembershipFormById(id).getCardStatus().equals("2")) {
            return error("已销户的卡不能再执行该操作");
        }
        return success(yqgMembershipFormService.selectYqgMembershipFormById(id));
    }

    /**
     * 新增会员
     */
    //@RequiresPermissions("project:form:add")
    @Log(title = "新增会员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqgMembershipForm yqgMembershipForm) {
        if (yqgMembershipForm.getCardSeller().equals("")) {
            LoginUser loginUser = getLoginUser();
            yqgMembershipForm.setCardSeller(loginUser.getSysUser().getNickName());
        }
        yqgMembershipForm.setCardStatus("1");
        return toAjax(yqgMembershipFormService.insertYqgMembershipForm(yqgMembershipForm));
    }

    /**
     * 修改会员
     */
    //@RequiresPermissions("project:form:edit")
    @Log(title = "修改会员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqgMembershipForm yqgMembershipForm) {
        return toAjax(yqgMembershipFormService.updateYqgMembershipForm(yqgMembershipForm));
    }

    /**
     * 删除会员
     */
    //@RequiresPermissions("project:form:remove")
    @Log(title = "删除会员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(yqgMembershipFormService.deleteYqgMembershipFormByIds(ids));
    }

    /**
     * 注销会员
     */
    @Log(title = "注销会员", businessType = BusinessType.UPDATE)
    @DeleteMapping("/del/{ids}")
    public void removeCard(@PathVariable Long[] ids) {
        for (Long id : ids) {
            YqgMembershipForm yqgMembershipForm = yqgMembershipFormService.selectYqgMembershipFormById(id);
            if (yqgMembershipForm.getCardStatus().equals("2")) {
                throw new ServiceException("已销户的卡不能再次销户");
            } else {
                yqgMembershipForm.setCardStatus("2");
                yqgMembershipForm.setAccountClosureTime(new Date());
                LoginUser loginUser = getLoginUser();
                yqgMembershipForm.setNote3(loginUser.getSysUser().getNickName());
                yqgMembershipFormService.updateYqgMembershipForm(yqgMembershipForm);
            }
        }
    }

    /**
     * 激活会员
     */
    @Log(title = "激活会员", businessType = BusinessType.UPDATE)
    @PostMapping("/active")
    public void activeCard(@RequestBody Long[] ids) {
        for (Long id : ids) {
            YqgMembershipForm yqgMembershipForm = yqgMembershipFormService.selectYqgMembershipFormById(id);
            if (yqgMembershipForm.getCardStatus().equals("2")) {
                throw new ServiceException("已销户的卡不能激活");
            } else if (yqgMembershipForm.getCardStatus().equals("1")) {
                yqgMembershipForm.setCardStatus("0");
                yqgMembershipFormService.updateYqgMembershipForm(yqgMembershipForm);
            }
        }
    }

    /**
     * 会员卡销售信息处理
     */
    public List<CardSell> handleSellInfo(long startTime, long endTime) {
        long startTimestamp = startTime + 9 * 60 * 60 * 1000;
        long endTimestamp = endTime + 9 * 60 * 60 * 1000;
        List<YqgMembershipForm> yqgMembershipFormList = yqgMembershipFormService.selectYqgMembershipFormList(new YqgMembershipForm());
        List<YqgMembershipForm> membershipFormList = new ArrayList<>();
        for (YqgMembershipForm yqgMembershipForm : yqgMembershipFormList) {
            if (yqgMembershipForm.getAccountOpeningTime().getTime() >= startTimestamp && yqgMembershipForm.getAccountOpeningTime().getTime() <= endTimestamp) {
                membershipFormList.add(yqgMembershipForm);
            }
        }
        if (membershipFormList.size() != 0) {
            CardSell cardSell00 = new CardSell().setType("0").setCardNature("0");
            CardSell cardSell01 = new CardSell().setType("0").setCardNature("1");
            CardSell cardSell02 = new CardSell().setType("0").setCardNature("2");
            CardSell cardSell04 = new CardSell().setType("0").setCardNature("4");
            CardSell cardSell13 = new CardSell().setType("1").setCardNature("3");
            for (YqgMembershipForm membershipForm : membershipFormList) {
                if (membershipForm.getType().equals("0") && membershipForm.getCardNature().equals("0")) {
                    cardSell00.setNumber(cardSell00.getNumber() + 1)
                            .setConsumableAmount(cardSell00.getConsumableAmount() + Long.parseLong(membershipForm.getNote2()));
                    if (membershipForm.getNote().equals("现金")) {
                        cardSell00.setCash(cardSell00.getCash() + Long.parseLong(membershipForm.getNote1()));
                    } else {
                        cardSell00.setBankcard(cardSell00.getBankcard() + Long.parseLong(membershipForm.getNote1()));
                    }
                    cardSell00.setNetReceipts(cardSell00.getCash() + cardSell00.getBankcard());
                } else if (membershipForm.getType().equals("0") && membershipForm.getCardNature().equals("1")) {
                    cardSell01.setNumber(cardSell01.getNumber() + 1)
                            .setConsumableAmount(cardSell01.getConsumableAmount() + Long.parseLong(membershipForm.getNote2()));
                    if (membershipForm.getNote().equals("现金")) {
                        cardSell01.setCash(cardSell01.getCash() + Long.parseLong(membershipForm.getNote1()));
                    } else {
                        cardSell01.setBankcard(cardSell01.getBankcard() + Long.parseLong(membershipForm.getNote1()));
                    }
                    cardSell01.setNetReceipts(cardSell01.getCash() + cardSell01.getBankcard());
                } else if (membershipForm.getType().equals("0") && membershipForm.getCardNature().equals("2")) {
                    cardSell02.setNumber(cardSell02.getNumber() + 1)
                            .setConsumableAmount(cardSell02.getConsumableAmount() + Long.parseLong(membershipForm.getNote2()));
                    if (membershipForm.getNote().equals("现金")) {
                        cardSell02.setCash(cardSell02.getCash() + Long.parseLong(membershipForm.getNote1()));
                    } else {
                        cardSell02.setBankcard(cardSell02.getBankcard() + Long.parseLong(membershipForm.getNote1()));
                    }
                    cardSell02.setNetReceipts(cardSell02.getCash() + cardSell02.getBankcard());
                } else if (membershipForm.getType().equals("0") && membershipForm.getCardNature().equals("4")) {
                    cardSell04.setNumber(cardSell04.getNumber() + 1)
                            .setConsumableAmount(cardSell04.getConsumableAmount() + Long.parseLong(membershipForm.getNote2()));
                    if (membershipForm.getNote().equals("现金")) {
                        cardSell04.setCash(cardSell04.getCash() + Long.parseLong(membershipForm.getNote1()));
                    } else {
                        cardSell04.setBankcard(cardSell04.getBankcard() + Long.parseLong(membershipForm.getNote1()));
                    }
                    cardSell04.setNetReceipts(cardSell04.getCash() + cardSell04.getBankcard());
                } else if (membershipForm.getType().equals("1") && membershipForm.getCardNature().equals("3")) {
                    cardSell13.setNumber(cardSell13.getNumber() + 1)
                            .setConsumableAmount(cardSell13.getConsumableAmount() + Long.parseLong(membershipForm.getNote2()));
                    if (membershipForm.getNote().equals("现金")) {
                        cardSell13.setCash(cardSell13.getCash() + Long.parseLong(membershipForm.getNote1()));
                    } else {
                        cardSell13.setBankcard(cardSell13.getBankcard() + Long.parseLong(membershipForm.getNote1()));
                    }
                    cardSell13.setNetReceipts(cardSell13.getCash() + cardSell13.getBankcard());
                }
            }
            List<CardSell> cardSellList = new ArrayList<>();
            CardSell cardSellCount0 = new CardSell().setCardNature("999");
            CardSell cardSellCount1 = new CardSell().setCardNature("999");

            doJudgment(cardSell00, cardSellList, cardSellCount0);
            doJudgment(cardSell01, cardSellList, cardSellCount0);
            doJudgment(cardSell02, cardSellList, cardSellCount0);
            doJudgment(cardSell04, cardSellList, cardSellCount0);

            if (cardSellCount0.getNumber() != 0) {
                cardSellList.add(cardSellCount0);
            }

            doJudgment(cardSell13, cardSellList, cardSellCount1);

            if (cardSellCount1.getNumber() != 0) {
                cardSellList.add(cardSellCount1);
            }

            return cardSellList;
        } else {
            return new ArrayList<>();
        }
    }

    public void doJudgment(CardSell cardSell, List<CardSell> cardSellList, CardSell cardSellCount) {
        if (cardSell.getNumber() != 0) {
            cardSellList.add(cardSell);
            cardSellCount.setNumber(cardSellCount.getNumber() + cardSell.getNumber())
                    .setNetReceipts(cardSellCount.getNetReceipts() + cardSell.getNetReceipts())
                    .setCash(cardSellCount.getCash() + cardSell.getCash())
                    .setBankcard(cardSellCount.getBankcard() + cardSell.getBankcard())
                    .setConsumableAmount(cardSellCount.getConsumableAmount() + cardSell.getConsumableAmount());
        }
    }

    /**
     * 会员卡销售查询
     */
    @PostMapping("/sell")
    //public void sell(@RequestBody Date[] timeList) {
    public TableDataInfo sell(@RequestBody SellRequestBody sellRequestBody) {
        //System.out.println(sellRequestBody.getStartTime().getTime() + "////" + sellRequestBody.getEndTime().getTime());
        long startTime = sellRequestBody.getStartTime().getTime();
        long endTime = sellRequestBody.getEndTime().getTime();
        return getDataTable(handleSellInfo(startTime, endTime));
    }

    /**
     * 导出会员销售列表
     */
    @Log(title = "导出会员销售表", businessType = BusinessType.EXPORT)
    @PostMapping("/export/sell")
    public void exportSell(HttpServletResponse response, YqgMembershipForm yqgMembershipForm) {
        //System.out.println(yqgMembershipForm.getNote1());
        //System.out.println(yqgMembershipForm.getNote2());
        List<CardSell> list = handleSellInfo(Long.parseLong(yqgMembershipForm.getNote1()), Long.parseLong(yqgMembershipForm.getNote2()));
        ExcelUtil<CardSell> util = new ExcelUtil<CardSell>(CardSell.class);
        util.exportExcel(response, list, "会员数据");
    }

    /**
     * 会员卡开户续款信息处理
     */
    public List<OpenAccount> handleOpenInfo(long startTime, long endTime, YqgMembershipForm yqgMembershipForm) {
        long startTimestamp = startTime + 9 * 60 * 60 * 1000;
        long endTimestamp = endTime + 9 * 60 * 60 * 1000;

        yqgMembershipForm.setCardStatus("0");
        List<YqgMembershipForm> yqgMembershipFormList = yqgMembershipFormService.selectYqgMembershipFormList(yqgMembershipForm);
        yqgMembershipForm.setCardStatus("1");
        List<YqgMembershipForm> yqgMembershipFormList1 = yqgMembershipFormService.selectYqgMembershipFormList(yqgMembershipForm);
        yqgMembershipFormList.addAll(yqgMembershipFormList1);

        // 小计1
        OpenAccount accountCount990 = new OpenAccount().setCardNature("990").setActualAmount("0").setConsumptionValue("0");
        // 小计2
        OpenAccount accountCountRecharge990 = new OpenAccount().setCardNature("990").setActualAmount("0").setConsumptionValue("0");
        // 收款总计
        OpenAccount accountCount991 = new OpenAccount().setCardNature("991").setActualAmount("0").setConsumptionValue("0");
        // 现金
        OpenAccount accountCount992 = new OpenAccount().setCardNature("992").setActualAmount("0");
        // 银行卡
        OpenAccount accountCount993 = new OpenAccount().setCardNature("993").setActualAmount("0");
        // 办卡总计
        OpenAccount accountCount994 = new OpenAccount().setCardNature("994").setActualAmount("0");
        // 续款总计
        OpenAccount accountCount995 = new OpenAccount().setCardNature("995").setActualAmount("0");
        // 序号计数
        long c = 1L;

        // 办卡信息处理
        List<String> cardNatures = new ArrayList<>();
        List<Long> amounts = new ArrayList<>();
        List<OpenAccount> accounts = new ArrayList<>();
        for (YqgMembershipForm membership : yqgMembershipFormList) {
            if (membership.getAccountOpeningTime().getTime() >= startTimestamp && membership.getAccountOpeningTime().getTime() <= endTimestamp) {
                // 变量声明
                OpenAccount account = new OpenAccount().setActualAmount("0").setConsumptionValue("0").setResidualAmount("0");
                account.setId(c + "")
                        .setCardNumber(membership.getCardNumber())
                        .setMemberCounterNumber(membership.getMemberCounterNumber())
                        .setType(membership.getType())
                        .setCardNature(membership.getCardNature())
                        .setName(membership.getName())
                        .setGiftCardSign(membership.getGiftCardSign())
                        .setActualAmount(membership.getActualAmount())
                        .setConsumptionValue(membership.getConsumptionValue())
                        .setCheckoutMethod(membership.getNote())
                        .setResidualAmount(membership.getResidualAmount())
                        .setAccountOpeningTime(membership.getAccountOpeningTime());
                accountCount990.setActualAmount((Long.parseLong(accountCount990.getActualAmount()) + Long.parseLong(account.getActualAmount())) + "")
                        .setConsumptionValue((Long.parseLong(accountCount990.getConsumptionValue()) + Long.parseLong(account.getConsumptionValue())) + "");
                if (membership.getNote().equals("银行卡")) {
                    accountCount993.setActualAmount((Long.parseLong(accountCount993.getActualAmount()) + Long.parseLong(account.getActualAmount())) + "");
                } else {
                    accountCount992.setActualAmount((Long.parseLong(accountCount992.getActualAmount()) + Long.parseLong(account.getActualAmount())) + "");
                }

                if (!cardNatures.contains(membership.getCardNature())) {
                    cardNatures.add(membership.getCardNature());
                    amounts.add(Long.parseLong(membership.getActualAmount()));
                } else {
                    int index = cardNatures.indexOf(membership.getCardNature());
                    amounts.set(index, amounts.get(index) + Long.parseLong(membership.getActualAmount()));
                }

                accounts.add(account);
                c += 1;
            }
        }
        if (!accountCount990.getActualAmount().equals("0") || !accountCount990.getConsumptionValue().equals("0")) {
            accounts.add(accountCount990);
        }

        c = 1L;
        // 续款信息处理
        List<String> reCardNatures = new ArrayList<>();
        List<Long> reAmounts = new ArrayList<>();
        YqgRechargeConsumption rechargeConsumption = new YqgRechargeConsumption();
        rechargeConsumption.setTypeOperation("0");
        rechargeConsumption.setCardNumber(yqgMembershipForm.getCardNumber());
        rechargeConsumption.setName(yqgMembershipForm.getName());
        rechargeConsumption.setMobilePhone(yqgMembershipForm.getMobilePhone());
        rechargeConsumption.setType(yqgMembershipForm.getType());
        rechargeConsumption.setCardNature(yqgMembershipForm.getCardNature());

        List<YqgRechargeConsumption> yqgRechargeConsumptions = yqgRechargeConsumptionService.selectYqgRechargeConsumptionList(rechargeConsumption);
        for (YqgRechargeConsumption yqgRechargeConsumption : yqgRechargeConsumptions) {
            if (yqgRechargeConsumption.getOperatingTime().getTime() >= startTimestamp && yqgRechargeConsumption.getOperatingTime().getTime() <= endTimestamp) {
                YqgMembershipForm form = yqgMembershipFormService.selectYqgMembershipFormByCardNumber(yqgRechargeConsumption.getCardNumber());
                // 变量声明
                OpenAccount account = new OpenAccount().setActualAmount("0").setConsumptionValue("0").setResidualAmount("0");
                account.setId(c + "")
                        .setCardNumber(yqgRechargeConsumption.getCardNumber())
                        .setMemberCounterNumber(form.getMemberCounterNumber())
                        .setType(yqgRechargeConsumption.getType())
                        .setCardNature(yqgRechargeConsumption.getCardNature())
                        .setName(yqgRechargeConsumption.getName())
                        .setGiftCardSign(form.getGiftCardSign())
                        .setActualAmount(yqgRechargeConsumption.getTopUpAmount())
                        //.setActualAmount("0")
                        .setConsumptionValue(yqgRechargeConsumption.getNote2())
                        .setCheckoutMethod(yqgRechargeConsumption.getPaymentMethod())
                        .setResidualAmount(form.getResidualAmount())
                        .setAccountOpeningTime(yqgRechargeConsumption.getOperatingTime());
                accountCountRecharge990.setActualAmount(Long.parseLong(accountCountRecharge990.getActualAmount()) + Long.parseLong(account.getActualAmount()) + "")
                        .setConsumptionValue((Long.parseLong(accountCountRecharge990.getConsumptionValue()) + Long.parseLong(account.getConsumptionValue())) + "");
                if (form.getNote().equals("银行卡")) {
                    accountCount993.setActualAmount((Long.parseLong(accountCount993.getActualAmount()) + Long.parseLong(account.getActualAmount())) + "");
                } else {
                    accountCount992.setActualAmount((Long.parseLong(accountCount992.getActualAmount()) + Long.parseLong(account.getActualAmount())) + "");
                }

                if (!reCardNatures.contains(form.getCardNature())) {
                    reCardNatures.add(form.getCardNature());
                    reAmounts.add(Long.parseLong(yqgRechargeConsumption.getTopUpAmount()));
                    //reAmounts.add(0L);
                } else {
                    int index = reCardNatures.indexOf(form.getCardNature());
                    reAmounts.set(index, reAmounts.get(index) + Long.parseLong(yqgRechargeConsumption.getTopUpAmount()));
                    //reAmounts.add(0L);
                }

                accounts.add(account);
                c += 1;
            }
        }
        if (!accountCountRecharge990.getActualAmount().equals("0") || !accountCountRecharge990.getConsumptionValue().equals("0")) {
            accounts.add(accountCountRecharge990);
        }

        accounts.add(accountCount991.setActualAmount((Long.parseLong(accountCount990.getActualAmount()) + Long.parseLong(accountCountRecharge990.getActualAmount())) + "")
                .setConsumptionValue((Long.parseLong(accountCount990.getConsumptionValue()) + Long.parseLong(accountCountRecharge990.getConsumptionValue())) + ""));
        accounts.add(accountCount992);
        accounts.add(accountCount993);
        accounts.add(new OpenAccount().setId(" "));

        accounts.add(accountCount994.setActualAmount(accountCount990.getActualAmount()));
        for (int i = 0; i < cardNatures.size(); i++) {
            accounts.add(new OpenAccount().setCardNature(cardNatures.get(i)).setActualAmount(amounts.get(i) + ""));
        }
        accounts.add(new OpenAccount().setId(" "));

        accounts.add(accountCount995.setActualAmount(accountCountRecharge990.getActualAmount()));
        for (int i = 0; i < reCardNatures.size(); i++) {
            accounts.add(new OpenAccount().setCardNature(reCardNatures.get(i)).setActualAmount(reAmounts.get(i) + ""));
        }

        return accounts;
    }

    /**
     * 会员卡开户续款查询
     */
    @PostMapping("/open")
    //public void sell(@RequestBody Date[] timeList) {
    public TableDataInfo open(@RequestBody CountRequestBody countRequestBody) {
        //System.out.println(sellRequestBody.getStartTime().getTime() + "////" + sellRequestBody.getEndTime().getTime());
        long startTime = countRequestBody.getStartTime().getTime();
        long endTime = countRequestBody.getEndTime().getTime();

        YqgMembershipForm yqgMembershipForm = new YqgMembershipForm();
        yqgMembershipForm.setCardNumber(countRequestBody.getCardNumber());
        yqgMembershipForm.setCardSeller(countRequestBody.getCardSeller());
        yqgMembershipForm.setCardNature(countRequestBody.getCardNature());

        return getDataTable(handleOpenInfo(startTime, endTime, yqgMembershipForm));
    }

    /**
     * 导出会员开户续款列表
     */
    @Log(title = "导出会员开户续款表", businessType = BusinessType.EXPORT)
    @PostMapping("/export/open")
    public void exportOpen(HttpServletResponse response, YqgMembershipForm yqgMembershipForm) {
        long startTime = Long.parseLong(yqgMembershipForm.getNote1());
        long endTime = Long.parseLong(yqgMembershipForm.getNote2());
        yqgMembershipForm.setNote1(null);
        yqgMembershipForm.setNote2(null);

        List<OpenAccount> list = handleOpenInfo(startTime, endTime, yqgMembershipForm);
        ExcelUtil<OpenAccount> util = new ExcelUtil<OpenAccount>(OpenAccount.class);
        util.exportExcel(response, list, "开户续款数据");
    }

    /**
     * 会员卡销户信息处理
     */
    public List<DelAccount> handleDelInfo(long startTime, long endTime, YqgMembershipForm yqgMembershipForm) {
        long startTimestamp = startTime + 9 * 60 * 60 * 1000;
        long endTimestamp = endTime + 9 * 60 * 60 * 1000;

        yqgMembershipForm.setCardStatus("2");
        List<YqgMembershipForm> yqgMembershipFormList = yqgMembershipFormService.selectYqgMembershipFormList(yqgMembershipForm);

        // 序号
        long c0 = 1L;
        long c1 = 1L;
        // 储值卡列表
        List<DelAccount> accounts0 = new ArrayList<>();
        // 次卡列表
        List<DelAccount> accounts1 = new ArrayList<>();
        // 储值卡小计
        DelAccount delAccount991 = new DelAccount().setCardNature("991").setResidualAmount("0");
        // 次卡小计
        DelAccount delAccount992 = new DelAccount().setCardNature("992").setResidualAmount("0");
        // 总计
        DelAccount delAccount993 = new DelAccount().setCardNature("993");

        for (YqgMembershipForm membershipForm : yqgMembershipFormList) {
            if (membershipForm.getAccountClosureTime().getTime() >= startTimestamp && membershipForm.getAccountClosureTime().getTime() <= endTimestamp) {
                if (membershipForm.getType().equals("0")) {
                    addAccount(c0, accounts0, delAccount991, membershipForm);
                    c0 += 1;
                } else if (membershipForm.getType().equals("1")) {
                    addAccount(c1, accounts1, delAccount992, membershipForm);
                    c1 += 1;
                }
            }
        }
        delAccount993.setResidualAmount((Long.parseLong(delAccount991.getResidualAmount()) + Long.parseLong(delAccount992.getResidualAmount())) + "");

        List<DelAccount> accounts = new ArrayList<>();
        if (accounts0.size() != 0) {
            accounts.addAll(accounts0);
            accounts.add(delAccount991);
        }
        if (accounts1.size() != 0) {
            accounts.addAll(accounts1);
            accounts.add(delAccount992);
        }
        if (accounts0.size() != 0 || accounts1.size() != 0) {
            accounts.add(delAccount993);
        }

        return accounts;
    }

    public void addAccount(long c, List<DelAccount> accountsS, DelAccount delAccount992, YqgMembershipForm membershipForm) {
        accountsS.add(new DelAccount().setId(c + "")
                .setCardNumber(membershipForm.getCardNumber())
                .setMemberCounterNumber(membershipForm.getMemberCounterNumber())
                .setName(membershipForm.getName())
                .setType(membershipForm.getType())
                .setCardNature(membershipForm.getCardNature())
                .setResidualAmount(membershipForm.getResidualAmount())
                .setOperator(membershipForm.getNote3())
                .setAccountClosureTime(membershipForm.getAccountClosureTime()));
        delAccount992.setResidualAmount((Long.parseLong(membershipForm.getResidualAmount()) + Long.parseLong(delAccount992.getResidualAmount())) + "");
    }

    /**
     * 会员卡销户查询
     */
    @PostMapping("/del")
    //public void sell(@RequestBody Date[] timeList) {
    public TableDataInfo del(@RequestBody CountRequestBody countRequestBody) {
        //System.out.println(sellRequestBody.getStartTime().getTime() + "////" + sellRequestBody.getEndTime().getTime());
        long startTime = countRequestBody.getStartTime().getTime();
        long endTime = countRequestBody.getEndTime().getTime();

        YqgMembershipForm yqgMembershipForm = new YqgMembershipForm();
        yqgMembershipForm.setCardNumber(countRequestBody.getCardNumber());
        yqgMembershipForm.setCardSeller(countRequestBody.getCardSeller());
        yqgMembershipForm.setCardNature(countRequestBody.getCardNature());

        return getDataTable(handleDelInfo(startTime, endTime, yqgMembershipForm));
    }

    /**
     * 导出会员销户列表
     */
    @Log(title = "导出会员销户表", businessType = BusinessType.EXPORT)
    @PostMapping("/export/del")
    public void exportDel(HttpServletResponse response, YqgMembershipForm yqgMembershipForm) {
        long startTime = Long.parseLong(yqgMembershipForm.getNote1());
        long endTime = Long.parseLong(yqgMembershipForm.getNote2());
        yqgMembershipForm.setNote1(null);
        yqgMembershipForm.setNote2(null);

        List<DelAccount> list = handleDelInfo(startTime, endTime, yqgMembershipForm);
        ExcelUtil<DelAccount> util = new ExcelUtil<DelAccount>(DelAccount.class);
        util.exportExcel(response, list, "开户续款数据");
    }
}
